#include "types.h"
#include "ports.h"
#include "macro.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <unixio.h>
#include <math.h>
#include <sys.h>
#include "msx2lib.h"
#include "map.h"

void uitpakker(uchar * input, uchar * output);
void vuitpakker(uchar * input, uint output) ;
void loadvramdata(void);

void SetPalet(void);

uchar *tstsc5;

void tst_tileset_sc5 (void) {

	tstsc5 = (uchar*) hidden_map;// malloc(9*1024);
	// if (tstsc5==NULL)
	// {
		// scr(0);
		// printf ("Error allocating tstsc5");
		// exit(1);
	// }
	loadvramdata();
    setpage(2);    
	vbit16 = 1;
    vuitpakker(tstsc5, 0);
	// free (tstsc5);
    SetPalet();
}

#asm
vdpport1 equ 0x99
vdpport2 equ 0x9A

levelcolors:
*include tiles\palette.asm

global _SetPalet

_SetPalet:   
	di
	xor a ;Set pointer to zero.
	out (vdpport1),a        
	ld  a,16 | 010000000B
	out (vdpport1),a

	ld  hl,levelcolors
	ld c,vdpport2
	rept 32
	outi
	endm
	ei
	ret

// _tstsc5:
// *include tiles\sc5.asm
#endasm





void loadvramdata(void)
{
    FILE * pFile;
	
	char namefile[] = "sc5.bin";
	
	pFile = fopen (namefile,"rb");
	if (pFile==NULL)
	{
		scr(0);
		printf ("Error opening file %s",namefile);
		exit(1);
	}
	else
	{
		fread (tstsc5,1,9*1024,pFile);	
		fclose (pFile);
	}

	return;
}

#asm
	global _bmove
_bmove:
; _d passed in de
	ex de,hl
; _s passed in bc
	ld	e,c
	ld	d,b
; _n passed on the stack
	pop	af
	pop	bc
	push af
	ldir
	ret
#endasm
